<template>

  <div class="mod-config">

    <basic-container>
      <navigation :sign="{a:false,b:true,c:false,d:false,e:false}"></navigation>
      <div class="boxsty zhiBiao">

        <div class="flex justifyContentSpaceBetween">
          <div class="tabletext">奖金公式管理</div>
          <div class="tablelink">
            <el-link icon="el-icon-plus" v-if="permissions.jiangjin_jiangjingspzb_add" type="primary"
                     @click="addOrUpdateHandle()" :underline="false" :disabled="deptId===''">新增奖金公式信息</el-link>
          </div>
        </div>
        <div style="margin-top:20px;width:100%">
          <span><b>负责部门：{{ deptName }}</b></span>
        </div>
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" class="form-inline">
          <el-form-item label="奖金公式名称">
            <el-input v-model="dataForm.gsMc" placeholder="请输入奖金公式名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" class="marginLeft" plain @click="getDataList()" :disabled="deptId===''">检索</el-button>
          </el-form-item>
        </el-form>
        <div class="avue-crud" style="max-height:calc(100vh - 490px)">
          <el-table :data="dataList" v-loading="dataListLoading" stripe border tooltip-effect="light">
            <el-table-column label="序号" header-align="center" align="center" type="index" width="80"></el-table-column>
            <el-table-column prop="gsFl" header-align="center" align="center" label="公式分类" min-width="100">
            </el-table-column>
            <el-table-column prop="gsMc" header-align="center" label="公式名称" min-width="150">
            </el-table-column>
            <el-table-column prop="gsZs" header-align="center" label="公式展示" min-width="450">
            </el-table-column>
            <el-table-column prop="gsYz" header-align="center" align="center" label="公式验证" min-width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.gsYz==0">验证不通过</span>
                <span v-else>验证已通过</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="300">
              <template slot-scope="scope">
                <el-button v-if="permissions.jiangjin_jiangjingspzb_edit" type="primary" plain @click="configHandle(scope.row.id)">配置</el-button>
                <el-button v-if="permissions.jiangjin_jiangjingspzb_edit" type="primary" plain @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                <el-button v-if="permissions.jiangjin_jiangjingspzb_del" type="danger" plain @click="deleteHandle(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="avue-crud__pagination">
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <!-- 弹窗, 配置 -->
      <config-form ref="configForm" @refreshDataList="getDataList"></config-form>
    </basic-container>
  </div>
</template>

<script>
  import {fetchList, delObj} from '@/api/jiangjin/jiangjingspzb'
  import TableForm from './jiangjingspzb-form'
  import configForm from './jiangjingspzb-config'
  import {mapGetters} from 'vuex'
  import {getJiangJinBm} from '@/api/admin/dept'

  export default {
    data () {
      return {
        dataForm: {
          gsMc: '',
          jjDeptId:''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        deptId: '',//负责部门ID
        deptName: '',//负责部门名称
      }
    },
    components: {
      TableForm,
      configForm
    },
    created () {

    },
    mounted() {
      this.getJiangJinJg();
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      //获取奖金管理人机构
      getJiangJinJg() {
        getJiangJinBm().then(res => {
          if (res.data.data != null && res.data.data.length > 0) {
            this.deptId = "";
            this.deptName = "";
            this.deptId = res.data.data[0].deptId;
            this.deptName = res.data.data[0].name;
            this.getDataList();
          } else {
            this.$notify.closeAll();
            //this.$notify.error("没有相关负责部门")
            //this.deptName = "无相关负责部门";
          }
        });
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$set(this.dataForm,"jjDeptId",this.deptId);
        fetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        }, this.dataForm)).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.deptId)
        })
      },
      //配置
      configHandle(id){
        this.$refs.configForm.init(id)
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(data => {
          this.$message.closeAll()
          this.$message.success('删除成功')
          this.getDataList()
        })
      }
    }
  }
</script>
